package com.meishij.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Paint;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.meishij.MallSellActivity;
import com.meishij.R;
import com.meishij.entry.MallEntry;

import java.util.List;

/**
 * Created by 肖荣 on 2016/10/13.
 */

public class MallEndListAdapter extends RecyclerView.Adapter<MallEndListAdapter.ViewHolder> {
    private Context context;
    private List<MallEntry.EndListBean> list;

    public MallEndListAdapter(Context context, List<MallEntry.EndListBean> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.layout_mall_end_list_item, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        holder.mall_end_list_item_image.setImageURI(list.get(position).getImage());
        holder.mall_end_list_item_title.setText(list.get(position).getTitle());
        holder.mall_end_list_item_price.setText("¥"+list.get(position).getPrice());
        holder.mall_end_list_item_market_price.setText("¥"+list.get(position).getMarket_price());
        holder.mall_end_list_item_market_price.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
        View parent = (View) holder.mall_end_list_item_image.getParent();
        parent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String id = list.get(position).getId();//详情页网络请求这个变为i
                String goodsSource = list.get(position).getGoodsSource();
                Intent intent=new Intent(context, MallSellActivity.class);
                intent.putExtra("i",id);
                intent.putExtra("goodsSource",goodsSource);
                context.startActivity(intent);
            }
        });
    }
    public void add(MallEntry.EndListBean item){
        list.add(item);
        notifyDataSetChanged();
    }
    public void addAll(List<MallEntry.EndListBean> endListBeanList){
        list.addAll(endListBeanList);
        notifyDataSetChanged();
    }
    public void clear(){
        list.clear();
        notifyDataSetChanged();
    }
    @Override
    public int getItemCount() {
        return list.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder{

        private SimpleDraweeView mall_end_list_item_image;
        private TextView mall_end_list_item_title;
        private TextView mall_end_list_item_price;
        private TextView mall_end_list_item_market_price;

        public ViewHolder(View itemView) {
            super(itemView);
            mall_end_list_item_image = ((SimpleDraweeView) itemView.findViewById(R.id.mall_end_list_item_image));
            mall_end_list_item_title = ((TextView) itemView.findViewById(R.id.mall_end_list_item_title));
            mall_end_list_item_price = ((TextView) itemView.findViewById(R.id.mall_end_list_item_price));
            mall_end_list_item_market_price = ((TextView) itemView.findViewById(R.id.mall_end_list_item_market_price));
        }
    }
}
